package sparrowGui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import sparrowGui.event.AlertEvent;
	import sparrowGui.utils.PubFun;
	
	/**
	 * 排队警告窗
	 * @author Pelephone
	 */
	public class QueueAlert extends BaseAlert
	{
		private var queue:Array;
		
		private var translucent:Sprite;			// 半透明背景
		private var _bgColor:int = 0x000000;		// 半透明背景的颜色
		private var _bgAlpha:Number = 0.5;			// 半透明的alpha值
		private var _bgRect:Rectangle;
		
		private var parentDP:DisplayObjectContainer;
		private var defaultAlert:AlertVO;
		
		public function QueueAlert(parent:DisplayObjectContainer,argSkin:Sprite=null, btnClass:Class=null)
		{
			parentDP = parent;
			super(null,BaseAlert.DEFAULT_BTN_STRING,argSkin, btnClass);
			autoClose = false;
			autoHidden = true;
		}
		
		override protected function init():void
		{
			PubFun.clearDisp(this);
			PubFun.SafeRemoveChild(this);
			queue = [];
			defaultAlert = new AlertVO();
			defaultAlert.newSkin = skin;
			translucent = new Sprite();
			setTranslucent();
			addChild(skin);
			PubFun.alignRect(translucent,this,"MC");
		}
		
		override public function createBtn(btnStr:String="确定|取消", btnCla:Class=null):void
		{
			if(defaultAlert.newBtnCls!=null && defaultAlert.newBtnCls==btnCla && defaultAlert.btnStr==btnStr)
				return;
			
			if(btnCla==null && defaultAlert.newBtnCls!=null) btnCla = defaultAlert.newBtnCls; 
			
			super.createBtn(btnStr,btnCla);
			defaultAlert.newBtnCls = btnCla;
		}
		
		override public function close():void
		{
			PubFun.SafeRemoveChild(this);
			PubFun.SafeRemoveChild(translucent);
			shiftRemoveAlertVO();
			if(queue.length){
				var avo:AlertVO = queue[(queue.length-1)] as AlertVO;
				showAlert(avo.data,avo.btnStr,avo.newSkin,avo.newBtnCls);
			}
		}
		
		/**
		 * 
		 * @param type
		 * @param listener
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 
		override public function addEventListener(type:String, listener:Function, 
												  useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			if(type==AlertEvent.ALERT_CLOSE && queue.length)
				(queue[(queue.length-1)] as AlertVO).addEventListener(type,listener,useCapture,priority,useWeakReference);
			else
				super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}*/
		
		override protected function sendClose(btnIndex:int, inpTxt:String):void
		{
//			(queue[(queue.length-1)] as AlertVO).dispatchEvent(new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt));
			super.sendClose(btnIndex,inpTxt);
			(queue[0] as AlertVO).dispatchEvent(new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt));
//			(queue[0] as AlertVO).backFun.apply(null,[new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt)]);
		}
		
		public function newSkin(panSkin:Sprite):void
		{
			if(!panSkin) return;
			PubFun.SafeRemoveChild(skin);
			_skin = panSkin;
			defaultAlert.newSkin = skin;
			PubFun.alignRect(translucent,this,"MC");
			addChild(skin);
		}
		
		public function alert(data:Object,btnStr:String="确定",backFun:Function=null,panSkin:Sprite=null,btnCls:Class=null):QueueAlert
		{
			addQueueAlert(data,btnStr,backFun,panSkin,btnCls);
			return showAlert(data,btnStr,panSkin,btnCls);
		}
		
		private function showAlert(data:Object,btnStr:String="确定",panSkin:Sprite=null,btnCls:Class=null):QueueAlert
		{
			newSkin(panSkin);
			createBtn(btnStr,btnCls);
			update(data);
			
			parentDP.addChild(translucent);
			parentDP.addChild(this);
			return this;
		}
		
		/**
		 * 增加提示队
		 * @param data
		 * @param btnStr
		 * @param panSkin
		 * @param btnCls
		 */
		private function addQueueAlert(data:Object,btnStr:String="确定",backFun:Function=null,panSkin:Sprite=null,btnCls:Class=null):void
		{
			var avo:AlertVO = new AlertVO();
			avo.data = data;
			avo.btnStr = btnStr;
			avo.newSkin = panSkin;
			avo.newBtnCls = btnCls;
//			avo.backFun = backFun;
			if(backFun!=null)
				avo.addEventListener(AlertEvent.ALERT_CLOSE,backFun);
			
			queue.push(avo);
		}
		
		/**
		 * 警告组当前警告窗添加监听事件
		 * @param type
		 * @param listener
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 */
		public function addEventQueue(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			if(!queue.length) return;
			(queue[0] as AlertVO).addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		/**
		 * 移出警告队列
		 * @return 
		 */
		private function shiftRemoveAlertVO():AlertVO
		{
			if(queue.length)
			{
				var avo:AlertVO = queue.shift() as AlertVO;
				PubFun.killObject(avo);
				avo.removeAllListeners();
				return avo;
			}
			else return null;
		}

		/**
		 * 设置半透明
		 * @param color
		 * @param alphpa
		 * @param rect
		 */
		public function setTranslucent(color:int=0x000000,talphpa:Number=0.3,rect:Rectangle=null):void
		{
			_bgColor = color;
			_bgAlpha = talphpa;
			_bgRect = rect;
			
			translucent.graphics.beginFill(_bgColor,_bgAlpha);
			
			if(_bgRect)
				translucent.graphics.drawRect(_bgRect.x,_bgRect.y,_bgRect.width,_bgRect.height);
			else
				translucent.graphics.drawRect(0,0,SparrowGUI.stageWidth,SparrowGUI.stageHeight);
				
			translucent.graphics.endFill();
		}
		
		override public function dispose():void
		{
			while(queue.length)
				shiftRemoveAlertVO();
			defaultAlert.removeAllListeners();
			PubFun.SafeRemoveChild(translucent);
			PubFun.SafeRemoveChild(skin);
			parentDP = null;
			
			super.dispose();
		}
	}
}
import flash.display.Sprite;

import sparrowGui.BaseAlert;
import sparrowGui.utils.EventDispatcherEx;

class AlertVO extends EventDispatcherEx{
	public var data:Object;
	public var idName:String;
	public var newSkin:Sprite;
	public var newBtnCls:Class;
//	public var backFun:Function;
	public var btnStr:String = BaseAlert.DEFAULT_BTN_STRING;
}